//
//  TempViewController.m
//  YSSmartHome
//
//  Created by Yahaong on 16/8/31.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "TempViewController.h"

@interface TempViewController ()
@property(nonatomic, weak) UIImageView *welcome;
@end

@implementation TempViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    UIWindow * window = [[UIApplication sharedApplication] keyWindow] ;
    UIImageView *welcome = [[UIImageView alloc]initWithFrame:window.bounds];
    [welcome setImage:[self launchImage]];
    [window addSubview:welcome];
    [window bringSubviewToFront:welcome];
    self.welcome = welcome;
}

- (UIImage *)getLaunchImageName {
    CGSize viewSize = [UIScreen mainScreen].bounds.size;
    // 竖屏
    NSString *viewOrientation = @"Portrait";
    NSString *launchImageName = nil;
    NSArray* imagesDict = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
    for (NSDictionary* dict in imagesDict) {
        CGSize imageSize = CGSizeFromString(dict[@"UILaunchImageSize"]);
        if (CGSizeEqualToSize(imageSize, viewSize) && [viewOrientation isEqualToString:dict[@"UILaunchImageOrientation"]]) {
            launchImageName = dict[@"UILaunchImageName"];
        }
    }
    return [UIImage imageNamed:launchImageName];
}

- (UIImage*)launchImage {
    UIView* launchView = [[UIStoryboard storyboardWithName:@"LaunchScreen" bundle:nil] instantiateInitialViewController].view;
    UIGraphicsBeginImageContext([UIScreen mainScreen].bounds.size);
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    [launchView.layer renderInContext:contextRef];
    UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
    return  image;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc {
    [self.welcome  removeFromSuperview];
    self.welcome = nil;
}

@end
